package flc.ast.fragment;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.AppUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.IconReplaceActivity;
import flc.ast.adapter.AppAdapter;
import flc.ast.bean.MyAppBean;
import flc.ast.databinding.FragmentAppBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.constant.Extra;
import stark.common.basic.utils.RxUtil;

public class AppFragment extends BaseNoModelFragment<FragmentAppBinding> {
    private int mType;
    private List<MyAppBean> mListShow = new ArrayList<>();
    private AppAdapter mAppAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_app;
    }

    public static AppFragment newInstance(int type) {
        AppFragment fragment = new AppFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(Extra.DATA, type);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    protected void initView() {
        mDataBinding.rvAppList.setLayoutManager(new GridLayoutManager(mContext, 4));
        mAppAdapter = new AppAdapter();
        mDataBinding.rvAppList.setAdapter(mAppAdapter);
        mAppAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        IconReplaceActivity.type=0;
        IconReplaceActivity.sAppBean = mAppAdapter.getItem(position);
        startActivity(IconReplaceActivity.class);
    }

    @Override
    protected void initData() {
        mType = getArguments().getInt(Extra.DATA);
        if (mType == 0) {
            getSystemIconListData();
        } else {
            getAllAppData();
        }

    }

    private void getAllAppData() {
        mListShow.clear();
        showDialog(getString(R.string.data_loading_hint));
        RxUtil.create(new RxUtil.Callback<List<PackageInfo>>() {
            @Override
            public void doBackground(ObservableEmitter<List<PackageInfo>> emitter) {
                List<PackageInfo> installedPackageInfoList = mContext.getPackageManager()
                        .getInstalledPackages(PackageManager.MATCH_UNINSTALLED_PACKAGES);
                emitter.onNext(installedPackageInfoList);
            }

            @Override
            public void accept(List<PackageInfo> infoList) {
                dismissDialog();
                if (infoList != null && infoList.size() != 0) {
                    for (PackageInfo info : infoList) {
                        if (!AppUtils.isAppSystem(info.packageName)) {
                            mListShow.add(new MyAppBean(info.packageName, info.applicationInfo.sourceDir, false));
                        }
                    }
                    mAppAdapter.setList(mListShow);
                    mDataBinding.rvAppList.setVisibility(View.VISIBLE);
                    mDataBinding.tvAppNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvAppList.setVisibility(View.GONE);
                    mDataBinding.tvAppNoData.setText(R.string.no_data);
                }
            }
        });
    }

    private void getSystemIconListData() {
        mListShow.clear();
        showDialog(getString(R.string.data_loading_hint));
        RxUtil.create(new RxUtil.Callback<List<PackageInfo>>() {
            @Override
            public void doBackground(ObservableEmitter<List<PackageInfo>> emitter) {
                List<PackageInfo> installedPackageInfoList = mContext.getPackageManager()
                        .getInstalledPackages(PackageManager.MATCH_UNINSTALLED_PACKAGES);
                emitter.onNext(installedPackageInfoList);
            }

            @Override
            public void accept(List<PackageInfo> infoList) {
                dismissDialog();
                if (infoList != null && infoList.size() != 0) {
                    for (PackageInfo info : infoList) {
                        if (AppUtils.isAppSystem(info.packageName)) {
                            mListShow.add(new MyAppBean(info.packageName, info.applicationInfo.sourceDir, false));
                        }
                    }
                    mAppAdapter.setList(mListShow);
                    mDataBinding.rvAppList.setVisibility(View.VISIBLE);
                    mDataBinding.tvAppNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvAppList.setVisibility(View.GONE);
                    mDataBinding.tvAppNoData.setText(R.string.no_data);
                }
            }
        });
    }
}